<template>
  <basic-container>
    <div class="avue-crud">
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item>
          <el-autocomplete
            clearable
            popper-class="el-autocomplete-suggestion"
            :popper-append-to-body="false"
            class="inline-input"
            v-model="queryParams.serviceName"
            :fetch-suggestions="querySearchAsync"
            placeholder="请输入内容"
            :trigger-on-focus="false"
            @select="handleSelect"
          >
            <template slot-scope="{ item }">
              <div class="autocomplete-choose-name" v-html="(item.highLightServiceName)"></div>
            </template>
          </el-autocomplete>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 操作工具栏 -->
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <!-- 列表页 -->
      <el-table v-loading="loading"
        :data="list">
        <el-table-column label="主键" align="center" prop="unid"/>
        <!--        <el-table-column label="分类主键" align="center" prop="catalogUnid" />-->
        <el-table-column label="事项编码" align="center" prop="serviceCode"/>
        <el-table-column label="事项名" align="center" prop="serviceName">
          <template slot-scope="scope">
            <span v-html="scope.row.serviceName"></span>
          </template>
        </el-table-column>
        <el-table-column label="热词" align="center" prop="keyWords"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)"
                       v-if="permissions.es_detail">详情
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                       v-if="permissions.es_edit">修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-if="permissions.es_del">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
                  @pagination="getList"/>
    </div>
  </basic-container>
</template>

<script>
import {getEsKeyWordList, getServiceList} from "@/api/es/setvicesearch";
import {mapGetters} from "vuex";

export default {
  name: "index",

  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        serviceName: null,
      },
      // 热词推荐搜索
      suggestKeyword: {
        current: 1,
        size: 10,
        keyword: null,
      },
      suggestList: [],
      // 结果列表
      list: [],
    }
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      // 执行查询
      getServiceList(params).then(response => {
        this.list = response.data.data.records;
        this.total = response.data.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.serviceName = null;
      this.handleQuery();
    },
    handleSelect(item) {
      this.queryParams.serviceName = item.serviceName;
    },
    querySearchAsync(queryString, cb) {
      this.getSuggestion(queryString, cb)
    },
    getSuggestion(keyword, cb) {
      this.loading = true;
      // 处理查询参数
      this.suggestKeyword.keyword = keyword;
      this.suggestKeyword.current = 0;

      let params = {...this.suggestKeyword};
      // 执行查询
      getEsKeyWordList(params).then(response => {
        cb(response.data.data.records)
        this.loading = false;
      });
    }
  }
}
</script>

<style scoped>

/deep/ .el-autocomplete-suggestion{
  width: auto!important;
}


</style>
